package com.yanqu.road.server.cmd.activity.springdinner.cross;

import com.yanqu.road.pb.activity.SpringDinnerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_CROSS_SPRINGDINNER_RP_TABLE_OPNE_SYNC, desc = "开桌子同步")
public class CrossSpringDinnerSyncOpenDataCmd extends AbstractCommand {

    private int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        List<Long> userIdList = new ArrayList<>();
        SpringDinnerProto.SpringDinnerOpenTableSyncMsg reqMsg = SpringDinnerProto.SpringDinnerOpenTableSyncMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        for(SpringDinnerProto.OpenTableTempMsg tableMsg : reqMsg.getTableList()){
            if(!userIdList.contains(tableMsg.getUserId())){
                userIdList.add(tableMsg.getUserId());
            }
        }
        List<GamePlayer> playerList = GamePlayerMgr.getAllOnlinePlayer();
        for(GamePlayer player : playerList){
            //不同步自己开的
            if(userIdList.contains(player.getUserId())){
                SpringDinnerProto.SpringDinnerOpenTableSyncMsg.Builder syncMsg = SpringDinnerProto.SpringDinnerOpenTableSyncMsg.newBuilder();
                syncMsg.setActivityId(activityId);
                for(SpringDinnerProto.OpenTableTempMsg tableMsg : reqMsg.getTableList()){
                    if(tableMsg.getUserId() != player.getUserId()){
                        syncMsg.addTable(tableMsg);
                    }
                }
                player.sendPacket(Protocol.U_SPRINGDINNER_RP_TABLE_OPNE_SYNC, syncMsg);
            }else{
                player.sendPacket(Protocol.U_SPRINGDINNER_RP_TABLE_OPNE_SYNC, reqMsg.toBuilder());
            }
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
